<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="classify">
        <el-select v-model="queryParams.classify" placeholder="分类" clearable size="small" style="width: 120px">
          <el-option
            v-for="classify in classifyOptions"
            :key="classify.value"
            :label="classify.label"
            :value="classify.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="展示状态" prop="showStatus">
        <el-select v-model="queryParams.showStatus" placeholder="展示状态" clearable size="small" style="width: 120px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          icon="el-icon-notebook-1"
          plain
          @click="queryCondition"
        >查看更多</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="blogList" size="mini" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="博客ID" align="center" prop="blogId" width="50" />
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" width="200" />
      <el-table-column label="摘要" align="center" prop="description" :show-overflow-tooltip="true" width="380" />
      <el-table-column label="博客分类" align="center" prop="classify" width="130">
        <template slot-scope="scope">
          <span>
            {{ classifyFormat(scope.row) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="显示状态" align="center" prop="copyrightStatus" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.showStatus"
            active-value="0"
            inactive-value="1"
            @change="handleShowStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="评论状态" align="center" prop="commentStatus" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.commentStatus"
            active-value="0"
            inactive-value="1"
            @change="handleCommentStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 预览博客 -->
    <el-dialog :visible.sync="open" width="800px" append-to-body>
      <div>
        <h2 style="text-align:center">
          {{ showBlog.title }}
        </h2>
        <div id="content" class="markdown-body" v-html="showBlog.content"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { dictType } from '@/api/system/dict/data'
import { getListByUser, deleteBlog, updateShow, updateComment } from '@/api/article/blog'
import 'highlight.js/scss/default.scss'
import 'highlight.js/styles/arduino-light.css'
import md from '@/utils/highlight'

export default {
  name: 'Blog',
  data() {
    return {
      loading: true,
      showSearch: true,
      single: true,
      multiple: true,
      total: 0,
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        classify: null,
        showStatus: null
      },
      showBlog: {},
      blogList: [], // 数据列表
      classifyOptions: [], // 分类列表
      statusOptions: [
        {
          value: '0',
          label: '展示'
        },
        {
          value: '1',
          label: '隐藏'
        }
      ],
      dateRange: []
    }
  },
  created() {
    this.getList()
    this.getClassifyList()
  },
  methods: {
    getList() {
      this.loading = true
      getListByUser(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        this.blogList = res.data
        this.total = res.total
        this.loading = false
      })
    },
    getClassifyList() {
      dictType('article_classify').then(res => {
        this.classifyOptions = res.data
      })
    },
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 删除
    handleDelete(row) {
      const blogIds = row.blogId || this.ids
      this.$confirm('是否确认删除博客编号为"' + blogIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return deleteBlog(blogIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    handleSelectionChange(selection) { // 多选框选中数据
      this.ids = selection.map(item => item.blogId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    classifyFormat(row) { // 博客分类字典翻译
      return this.selectDictLabel(this.classifyOptions, row.classify)
    },
    handleShowStatusChange(row) { // 展示状态开关
      const text = row.showStatus === '0' ? '展示' : '隐藏'
      this.$confirm('确认要"' + text + '"《"' + row.title + '"》吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return updateShow(row.blogId, row.showStatus)
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.showStatus = row.showStatus === '0' ? '1' : '0'
      })
    },
    handleCommentStatusChange(row) { // 评论状态开关
      const text = row.commentStatus === '0' ? '开启' : '关闭'
      this.$confirm('确认要"' + text + '"《"' + row.title + '"》的评论区吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return updateComment(row.blogId, row.commentStatus)
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.commentStatus = row.commentStatus === '0' ? '1' : '0'
      })
    },
    handlePreview(row) { // 预览
      this.showBlog = row
      this.showBlog.content = md.render(row.content)
      this.open = true
    },
    handleUpdate(row) { // 修改
      this.$router.push({ path: '/blog/add/' + row.blogId })
    },
    handleAdd() { // 新增
      this.$router.push({ path: '/blog/add/' + 0 })
    },
    queryCondition() { // 查看更多
      this.$router.push({ path: '/article' })
    }
  }
}
</script>

<style scoped>

</style>
